package com.jiexinyun.dto.response;

import com.alibaba.fastjson2.JSON;
import com.jiexinyun.constants.HttpStatus;
import com.jiexinyun.error.ErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.Objects;

@Slf4j
@Data
@Schema(name = "AjaxResult", description = "通用返回对象")
public class AjaxResult implements Serializable {

    @Schema(name = "code", description = "状态码", example = "200")
    private int code;

    @Schema(name = "message", description = "消息", example = "操作成功")
    private String message;

    @Schema(name = "developerMessage", description = "开发者信息", example = "SQL错误")
    private String developerMessage;

    @Schema(name = "data", description = "数据")
    private Object data;

    private static final int SUCCESS_CODE = 200;
    private static final int ERROR_CODE = 500;

    private static final String SUCCESS_MESSAGE = "操作成功";
    private static final String ERROR_MESSAGE = "操作失败";

    public AjaxResult() {
        this.code = ERROR_CODE;
        this.message = ERROR_MESSAGE;
    }

    public AjaxResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AjaxResult(String developerMessage) {
        this.code = ERROR_CODE;
        this.message = ERROR_MESSAGE;
        this.developerMessage = developerMessage;
    }

    public AjaxResult(String message, String developerMessage) {
        this.code = ERROR_CODE;
        this.message = message;
        this.developerMessage = developerMessage;
    }

    public AjaxResult(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        if(data != null) {
            this.data = data;
        }
    }

    public AjaxResult(int code, String message, String developerMessage, Object data) {
        this.code = code;
        this.message = message;
        this.developerMessage = developerMessage;
        if(data != null) {
            this.data = data;
        }
    }

    public static AjaxResult success() {
        return AjaxResult.success(SUCCESS_MESSAGE, null);
    }

    public static AjaxResult success(Object data) {
        return AjaxResult.success(SUCCESS_MESSAGE, data);
    }

    public static AjaxResult success(String message, Object data) {
        return new AjaxResult(HttpStatus.SUCCESS, message, data);
    }

    //这个方法是为了给前端看的开发错误信息
    public static AjaxResult developerError(String developerMessage) {
        return AjaxResult.error(ERROR_CODE, ERROR_MESSAGE, developerMessage, null);
    }

    //这个方法是为了给前端看的开发错误信息
    public static AjaxResult developerError(String developerMessage, Object data) {
        return AjaxResult.error(ERROR_CODE, ERROR_MESSAGE, developerMessage, data);
    }

    public static AjaxResult error() {
        return AjaxResult.error(ERROR_MESSAGE);
    }

    public static AjaxResult error(String message) {
        return AjaxResult.error(message, null);
    }

    public static AjaxResult error(int code, String message, String developerMessage, Object data) {
        return new AjaxResult(code, message, developerMessage, data);
    }

    public static AjaxResult error(String message, Object data) {
        log.info(message);
        return new AjaxResult(HttpStatus.ERROR, message, data);
    }

    public static AjaxResult error(int code, String message) {
        return new AjaxResult(code, message, null);
    }

    public static AjaxResult error(int code) {
        return new AjaxResult(code, ERROR_MESSAGE, null);
    }

    public static AjaxResult error(ErrorCode errorCode) {
        log.info(errorCode.getMessage());
        return new AjaxResult(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public boolean ifSuccess() {
        return Objects.equals(HttpStatus.SUCCESS, this.code);
    }

    public boolean ifError() {
        return !Objects.equals(HttpStatus.SUCCESS, this.code);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
